//
//  ViewController.m
//  Stock_List
//
//  Created by Canny on 2017/5/29.
//  Copyright © 2017年 HZJK. All rights reserved.
//

#import "ViewController.h"
#import "UIView+customView.h"
#import "Masonry.h"
#import "Left_cell.h"
#import "Right_Cell.h"
#import "Stock.h"
#define LeftTableViewWidth 100
#define RightLabelWidth 80

#define ScreenBounds [[UIScreen mainScreen] bounds]
#define ScreenWidth ScreenBounds.size.width
#define ScreenHeight ScreenBounds.size.height



@interface ViewController ()
<

UITableViewDataSource,
UITableViewDelegate

>
@property (nonatomic,strong) UITableView *tableV_left;
@property (nonatomic,strong) UITableView *tableV_right;

@property (nonatomic,strong) UIScrollView *ScrollV_right;
@property (nonatomic,strong) NSArray *arr_rightTitles;
@property (nonatomic,strong) NSArray *customStocks;
@end

@implementation ViewController

#pragma mark - 懒加载属性
- (NSArray *)customStocks{
    if (!_customStocks) {
        NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"customStocks.plist" ofType:nil]];
        NSMutableArray *mutableArray = [NSMutableArray array];
        for (NSDictionary *dic in array) {
            Stock *stock = [Stock new];
            [stock setValuesForKeysWithDictionary:dic];
            [mutableArray addObject:stock];
        }
        
        
        _customStocks = mutableArray;
    }
    return _customStocks;
}

#pragma 右侧列表项
-(NSArray *)arr_rightTitles{
    if (!_arr_rightTitles) {
        _arr_rightTitles = @[@"品种", @"最新价", @"涨跌", @"涨跌幅", @"买一", @"买量", @"卖一", @"卖量",@"成交量",@"持仓量",@"成交额"];
    }
    return _arr_rightTitles;
}


#pragma mark - 设置主页面
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self createUI];

}
#pragma mark==================createUI===================
-(void)createUI{
    self.title=@"全球行情";
    self.view.backgroundColor=[UIColor  whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.automaticallyAdjustsScrollViewInsets = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
        self.navigationController.navigationBar.clipsToBounds = NO;
    }
    
    
    [self load_Left_TableV];
    [self load_Right_TableV];
    [self load_ScrollV_right];

}



//设置分割线顶格
- (void)viewDidLayoutSubviews{
    [self.tableV_left setLayoutMargins:UIEdgeInsetsZero];
    [self.tableV_right setLayoutMargins:UIEdgeInsetsZero];
}
/*
 * 加载左边的TableView
 */
- (void)load_Left_TableV{
    self.tableV_left = [[UITableView alloc] init];
    self.tableV_left.delegate = self;
    self.tableV_left.dataSource = self;
    self.tableV_left.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.tableV_left.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.tableV_left];
    self.tableV_left.frame=CGRectMake(0, 0, LeftTableViewWidth,ScreenHeight);
    
}
/*
 * 加载右边的TableView
 */
- (void)load_Right_TableV{
    self.tableV_right = [[UITableView alloc] init ];
    self.tableV_right.delegate = self;
    self.tableV_right.dataSource = self;
    self.tableV_right.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.tableV_right.showsVerticalScrollIndicator = NO;
    self.tableV_right.frame=CGRectMake(0, 0, self.arr_rightTitles.count * RightLabelWidth + 20, ScreenHeight);

}
/*
 * 加载右边的ScrollV
 */
- (void)load_ScrollV_right{
    
    self.ScrollV_right = [[UIScrollView alloc] init];
    self.ScrollV_right.contentSize = CGSizeMake(self.tableV_right.bounds.size.width, 0);
    self.ScrollV_right.backgroundColor = [UIColor clearColor];
    self.ScrollV_right.bounces = NO;
    self.ScrollV_right.showsHorizontalScrollIndicator = NO;
    
    [self.ScrollV_right addSubview:self.tableV_right];
    [self.view addSubview:self.ScrollV_right];
    
    [self.ScrollV_right mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(0);
        make.right.and.bottom.equalTo(self.view);
        make.left.equalTo(self.tableV_left.mas_right);
    }];
    
}

#pragma mark - table view dataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.tableV_left) {
        static NSString *reuseIdentifer = @"Left_cell";
        Left_cell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifer];
        if(!cell){
            cell = [[Left_cell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifer];
            [self resetSeparatorInsetForCell:cell];
        }

        Stock *stock = self.customStocks[indexPath.row % 7];
        [cell relayOutWithModel:stock];
        
        return cell;
    }else{
        static NSString *reuseIdentifer = @"Right_Cell";
        Right_Cell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifer];
        if(!cell){
            cell = [[Right_Cell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifer];
            [self resetSeparatorInsetForCell:cell];
        }
        
        [cell relayOutWithModel:self.arr_rightTitles];

        //这里先使用假数据
        UIView *view = [cell viewWithTag:100];
        for (UILabel *label in view.subviews) {
            label.text = nil;
            label.text = [NSString stringWithFormat:@"%ld", indexPath.row];
        }
        return cell;
    }
}
//设置cell分割线顶格
- (void)resetSeparatorInsetForCell:(UITableViewCell *)cell {
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
        return 10;
}
#pragma mark -- 设置左右两个table View的自定义头部View
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (tableView == self.tableV_right) {
        UIView *rightHeaderView = [UIView viewWithLabelNumber:self.arr_rightTitles.count];
        int i = 0;
        for (UILabel *label in rightHeaderView.subviews) {
            label.text = self.arr_rightTitles[i++];
            label.textAlignment=NSTextAlignmentCenter;
        }
        rightHeaderView.backgroundColor = [UIColor lightGrayColor];
        return rightHeaderView;
    }else{
        UILabel * lab=[[UILabel alloc]init];
        lab.text=@"自选";
        lab.textAlignment=NSTextAlignmentCenter;
        lab.backgroundColor=[UIColor lightGrayColor];
        return lab;
    }
}
//必须实现以下方法才可以使用自定义头部
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

#pragma mark - table view delegate
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.tableV_right selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    [self.tableV_left selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
}

#pragma mark - 两个tableView联动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == self.tableV_left) {
        [self tableView:self.tableV_right scrollFollowTheOther:self.tableV_left];
    }else{
        [self tableView:self.tableV_left scrollFollowTheOther:self.tableV_right];
    }
}

- (void)tableView:(UITableView *)tableView scrollFollowTheOther:(UITableView *)other{
    CGFloat offsetY= other.contentOffset.y;
    CGPoint offset=tableView.contentOffset;
    offset.y=offsetY;
    tableView.contentOffset=offset;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"点击了%ld----%ld",(long)indexPath.section,(long)indexPath.row);
   
}


@end
